package com.lyz.myself.controller;

import com.lyz.myself.config.utils.Result;
import com.lyz.myself.pojo.Po.Position;
import com.lyz.myself.pojo.Po.RoleDepartment;
import com.lyz.myself.pojo.Vo.PositionVo;
import com.lyz.myself.service.PositionService;
import com.lyz.myself.service.RoleDepartmentService;
import com.lyz.myself.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(description="组织架构")
@RestController
@RequestMapping("/branch")
public class BranchController extends BaseController{

    @Resource
    private RoleDepartmentService roleDepartmentService;

    @Resource
    private PositionService positionService;

    @Resource
    private UserService userService;

    @ApiOperation("部门岗位图")
    @RequiresAuthentication
    @PostMapping("/menu")
    public Result menu(){
        List<RoleDepartment> roleDepartmentListTemp = roleDepartmentService.menu(getUser().getCompany_id());
        List<RoleDepartment> roleDepartmentList = this.buildTree(roleDepartmentListTemp,0);
        return Result.success(roleDepartmentList,"查询成功");
    }

    @ApiOperation("上下级关系图")
    @RequiresAuthentication
    @PostMapping("/positionList")
    public Result positionList(){
        List<Position> positionListTemp = positionService.positionList(getUser().getCompany_id());
        List<Position> positionList = this.positionTree(positionListTemp,0);
        List<PositionVo> positionVoList = ejbGenerator.convert(positionList, PositionVo.class);
        return Result.success(positionVoList,"查询成功");
    }

    @ApiOperation("单位人员管理")
    @RequiresAuthentication
    @PostMapping("/userList")
    public Result userList(
            @RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize
    ){
        return Result.success( userService.myUser(getUser().getCompany_id(),pageNum,pageSize),"查询成功");
    }

    public static List<RoleDepartment> buildTree(List<RoleDepartment> list, Integer parentId) {
        List<RoleDepartment> menus = new ArrayList<RoleDepartment>();
        for (RoleDepartment menu : list) {
            Integer menuId = menu.getDepartment_id();
            Integer pid = menu.getParent_id();
            if (parentId==pid) {
                List<RoleDepartment> menuLists = buildTree(list, menuId);
                menu.setChildren(menuLists);
                menus.add(menu);
            }
        }
        return menus;
    }

    public static List<Position> positionTree(List<Position> list, Integer parentId) {
        List<Position> menus = new ArrayList<Position>();
        for (Position menu : list) {
            Integer menuId = menu.getPosition_id();
            Integer pid = menu.getParent_id();
            if (parentId==pid) {
                List<Position> menuLists = positionTree(list, menuId);
                menu.setChildren(menuLists);
                menus.add(menu);
            }
        }
        return menus;
    }
}
